package ManipullateDatabases;

import ConnectionsDatebases.Dbutils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OperatorCType {
    public static void insert(String typename) {
        Connection connect = Dbutils.Connect();
        String sql = "insert into cartype values (null,'" + typename + "',0);";
        PreparedStatement preparedStatement = Dbutils.getPrepareStatement(connect, sql);
        try {
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Dbutils.closePreparestatement(preparedStatement);
        Dbutils.closeConnection(connect);
    }

    public static int getTypeId(String typeName) {
        Connection connect = Dbutils.Connect();
        int typeId = 0;
        String sql = "SELECT typeId FROM cartype WHERE typeName ='" + typeName + "';";
        PreparedStatement prepareStatement = Dbutils.getPrepareStatement(connect, sql);
        ResultSet resultSet = null;
        try {
            resultSet = prepareStatement.executeQuery();
            while (resultSet.next()) {
                typeId = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (resultSet != null) {
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        Dbutils.closeStatement(prepareStatement);
        Dbutils.closeConnection(connect);
        return typeId;
    }
    public static List<String> getCatType(){
        List<String> types = new ArrayList<>();
        Connection connect = Dbutils.Connect();
        String sql = "SELECT typeName FROM cartype;";
        ResultSet rs = null;
        PreparedStatement prepareStatement = Dbutils.getPrepareStatement(connect, sql);
        try{
            int i=0;
            rs = prepareStatement.executeQuery();
            while(rs.next()){
                types.add(rs.getString(1));
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return types;
    }
}
